/**重置message，防止重复点击重复弹出message弹框 */
import {Message} from 'element-ui';

let messageInstance = null;
const resetMessage = (options) => {
   if (messageInstance) {
     messageInstance.close()
   }
   messageInstance = Message(options)
};
['error', 'success', 'info', 'warning'].forEach(type => {
	resetMessage[type] = options => {
		 if (typeof options === 'string') {
		   options = {
		     message: options
		   }
		 }
	 options.type = type
	 return resetMessage(options)
	}
})
export const message = resetMessage



export function filterSize(size) {
  if (!size) return '-'
  if (size < pow1024(1)) return size + ' B'
  if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB'
  if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB'
  if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB'
  return (size / pow1024(4)).toFixed(2) + ' TB'
}

function pow1024(num) {
  return Math.pow(1024, num)
}



export default {
  errroImg(){
    return require("@/assets/images/noheadimg.png");
  },

  imgError(event) {
    let img = event.srcElement;
    img.src = require("@/assets/images/sunh.png");
    img.onerror = null; //防止闪图

    return require("@/assets/images/sunh.png");
  },
  coverError(event) {
    let img = event.srcElement;
    img.src = require("@/assets/images/form-banner.png");
    img.onerror = null; //防止闪图

    return require("@/assets/images/form-banner.png");
  },
  
  exportExcel(res) {
    // 导出
    const loadingInstance = this.$loading({
      lock: true,
      text: '正在导出，请稍候...',
      background: 'rgba(0, 0, 0, 0.7)'
    });
    const blob = new Blob([res], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
    });

    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(blob, "");
    } else {
      let url = window.URL.createObjectURL(blob);
      let link = document.createElement("a");
      const timestamp = new Date().getTime();
      link.download = timestamp;
      link.style.display = "none";
      link.href = url;
      document.body.appendChild(link);
      link.click();
    }
    loadingInstance.close();
    this.$message({
      message: "导出成功",
      type: "success",
    });
  },

  isJurisdiction(id){
    
    if(id){
      let admin_id = this.$local.get('id');
      if(admin_id === 1){
        return true;
      };


      if(id === admin_id){
        return true;
      }else{
        return false;
      }
    }
  },
};